package com.rent.appointment.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rent.appointment.annotation.Perm;
import com.rent.appointment.dto.ApplyViewDto;
import com.rent.appointment.entity.ApplyView;
import com.rent.appointment.service.AppointmentService;
import com.rent.common.entity.ResponseResult;
import com.rent.common.interceptor.UserContext;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(tags = "预约接口")
@RequestMapping("/api/appointment")
@Validated
public class AppointmentController {
    @Autowired
    private AppointmentService appointmentService;

    @ApiOperation(value = "创建预约",httpMethod = "POST")
    @PostMapping("/create")
    public ResponseResult create(@RequestBody ApplyViewDto applyViewDto){
        appointmentService.createAppointment(applyViewDto);
        return ResponseResult.success("创建预约成功",null);
    }

    @ApiOperation(value = "我的预约列表",httpMethod ="GET")
    @GetMapping("/myList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "分页数", dataType = "Integer",
                    paramType = "query", required = false, defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", dataType = "Integer",
                    paramType = "query", required = false, defaultValue = "10")
    })
    public ResponseResult queryMylist(Integer pageNo, Integer pageSize){
        Page<ApplyView> page = new Page<>(pageNo,pageSize);
        LambdaQueryWrapper<ApplyView> queryWrapper = new LambdaQueryWrapper<>();
        //从UserContext获取当前用户的userid进行查询
        queryWrapper.eq(ApplyView::getUserId, UserContext.getUserId());
        Page<ApplyView> list = appointmentService.getBaseMapper().selectPage(page,queryWrapper);
        return ResponseResult.success("预约列表如下:",list);
    }

    @Perm("landlord")
    @ApiOperation(value = "房东预约列表",httpMethod = "GET")
    @GetMapping("/landlordList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "分页数", dataType = "Integer",
                    paramType = "query", required = false, defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", dataType = "Integer",
                    paramType = "query", required = false, defaultValue = "10")
    })
    public ResponseResult querylandlordList(Integer pageNo, Integer pageSize){
        Page<ApplyView> page = new Page<>(pageNo,pageSize);
        LambdaQueryWrapper<ApplyView> queryWrapper = new LambdaQueryWrapper<>();
        //从UserContext获取当前用户的userid进行查询,查询
        queryWrapper.eq(ApplyView::getLandlordId, UserContext.getUserId());
        Page<ApplyView> list = appointmentService.getBaseMapper().selectPage(page,queryWrapper);
        return ResponseResult.success("房东列表如下:",list);
    }

    @ApiOperation(value = "预约详情",httpMethod = "GET")
    @GetMapping("/{id}")
    public ResponseResult viewDetails(@PathVariable Long id){
        ApplyView details = appointmentService.getById(id);
        return ResponseResult.success("预约详情如下：",details);
    }

    @Perm("landlord")
    @ApiOperation(value = "处理预约",httpMethod = "PUT")
    @PutMapping("/handle")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "预约号码",
                    paramType = "query", required = true),
            @ApiImplicitParam(name = "status", value = "状态（1-同意，2-拒绝）",
                    paramType = "query", required = true)
    })
    public ResponseResult handleView(Long id, Integer status){
        appointmentService.handleView(id, status);
        return ResponseResult.success("修改预约成功！",null);
    }

    @ApiOperation(value = "取消预约",httpMethod = "PUT")
    @PutMapping("/cancel")
    public ResponseResult cancelView(@RequestParam Long id){
        appointmentService.deleteView(id);
        return ResponseResult.success("取消预约成功!",null);
    }
}
